<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
<!--      <el-form-item label="运单号" prop="waybillNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.waybillNum"-->
<!--          placeholder="请输入运单号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="出场时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="车牌号" prop="carNo">
        <el-input
          v-model="queryParams.carNo"
          placeholder="请输入车牌号码或者挂车号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属场站" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入所属场站"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾驶员" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入驾驶员姓名或联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item  label="所属区域" prop="areaCode">
        <el-select v-model="queryParams.areaCode"  placeholder="请选择">
          <el-option
            v-for="item in townData"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="监管单位" prop="supervisionUnit">
        <el-select v-model="queryParams.supervisionUnit" placeholder="请选择" clearable>
          <el-option
            v-for="dict in station_jgdw"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="handleExport"
          v-hasPermi="['weight:waybill:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="waybillList" :max-height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="运单号" align="center" prop="waybillNum" />-->
      <el-table-column label="所属场站" align="left" prop="stationName" width="260" />
      <el-table-column label="监管单位" align="center" prop="supervisionUnit" :formatter="supervisionUnitFormat" />
      <el-table-column label="车牌号" align="center" prop="carNo"  />
      <el-table-column label="挂车车牌" align="center" prop="carNoG"  />
      <el-table-column label="货物总重kg" align="center" prop="totalWeight"  />
      <el-table-column label="称重总重kg" align="center" prop="grossWeight">
        <template slot-scope="scope">
          <span v-if="scope.row.grossWeight==null" style="color: red">未称重</span>
          <span v-else>{{scope.row.grossWeight}}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾驶员" align="center" prop="driverName"  />
      <el-table-column label="联系电话" align="center" prop="driverPhone" />
      <el-table-column label="发起时间" align="center" prop="initiationTime"  width="180"/>
      <el-table-column label="出场确认时间" align="center" prop="sureTime" width="180"/>
      <el-table-column label="所属区域" align="center" prop="areaCode" :formatter="converJurisdictionUnit"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['weight:waybill:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listWaybill, getWaybill, delWaybill, exportWaybill, exportWaybill2 } from "@/api/weight/waybill";
  import { getTown, getAreaListByUserId } from "@/api/weight/station";
  export default {
    name: "Waybill",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 运单表格数据
        waybillList: [],
        // 认证状态
        dict_waybill_status: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //日期范围
        dateRange: [],
        //字典-监管单位
        station_jgdw: [],
        townData: [],
        is_admin: false,
        is_leave: true,
        tableHeight: window.innerHeight - 330,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          waybillNum: undefined,
          stationName: undefined,
          carNo: undefined,
          driverName: undefined,
          isAbnormal: undefined,
          hasExit: undefined,
          areaCode: undefined,
          // supervisionUnit:undefined,
        },
        // 表单参数
        form: {}
      };
    },
    created() {
      let date = new Date();
      let today= this.timeFormat(date) //当前日期
      // 默认日期赋值
      this.dateRange = [today, today];

      const  hasexit=this.$route.query.hasExit;
      this.queryParams.hasExit = hasexit;

      this.getList();
      // 认证状态
      this.getDicts("waybill_status").then(response => {
        this.dict_waybill_status = response.data;
      });
      this.getDicts("station_jgdw").then((response) => {
        this.station_jgdw = response.data;
      });
      const roles = this.$store.getters && this.$store.getters.roles;
      for (let rol of roles){
        if ('admin'==rol){
          this.is_admin=true;
        }
      }

      getAreaListByUserId().then(response =>{
        this.townData=response.data;
      });

    },
    methods: {
      // 岗位状态字典翻译
      supervisionUnitFormat(row, column) {
        return this.selectDictLabel(this.station_jgdw, row.supervisionUnit);
      },
      /** 查询运单列表 */
      getList() {
        if(this.dateRange == null || this.dateRange.length  <= 0){
          this.msgInfo("请选择查询日期区间");
          return;
        }
        this.loading = true;
        listWaybill(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.waybillList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          waybillId: undefined,
          waybillNum: undefined,
          stationName: undefined,
          carNo: undefined,
          driverName: undefined,
          isAbnormal: undefined
        };
        this.resetForm("form");
      },
      /** 获取管辖地，属地 **/
      getTown(areaCode) {
        getTown(areaCode).then(response =>{
          this.townData=response.data;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        let date = new Date();
        let today= this.timeFormat(date) //当前日期
        // 默认日期赋值
        this.dateRange = [today, today];
        this.handleQuery();
      },
      /** 详情按钮操作 */
      handleView(row) {
        this.reset();
        const waybillId = row.waybillId || this.ids
        this.$router.push({
          path:"/weigh/waybilldetail",
          query:{
            id:waybillId
          }
        })
          console.log(this.$router);
        // getWaybill(waybillId).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "运单详情";
        // });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.is_leave = true;
        this.ids = [];
        if(selection.length > 0){
          for (let s of selection){
            if(s.hasExit != '1'){
              this.is_leave = false;
            }
            this.ids.push(s.waybillId);
          }
        }
        if(!this.is_leave){
          this.msgError("请选择已确认出场的运单导出");
          this.multiple = true;
          return false;
        }
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.waybillId;
        console.info(id)
        this.$confirm('是否确认删除运单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWaybill(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      // handleExport() {
      //   const queryParams = this.queryParams;
      //   this.$confirm('是否确认导出所有运单数据项?', "警告", {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       type: "warning"
      //     }).then(function() {
      //       return exportWaybill(queryParams);
      //     }).then(response => {
      //       this.download(response.msg);
      //     }).catch(function() {});
      // }
      handleExport() {
        const ids = this.ids;
        this.$confirm('是否确认导出所选运单的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWaybill2(ids);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
      typeFormat(row,column){
        if(row.grossWeight != null && row.grossWeight != ''){
          return row.grossWeight;
        }else{
          return '未称重';
        }
      },
      timeFormat: function (date) {
        if (!date || typeof(date) === "string") {
          this.error("参数异常，请检查...");
        }
        var y = date.getFullYear(); //年
        var m = date.getMonth() + 1; //月
        var d = date.getDate(); //日

        return y + "-" + (m < 10 ? '0' + m : m) + "-" + (d < 10 ? '0' + d : d);
      },
      // 所属区域转换
      converJurisdictionUnit(row,column){
        let areaName = '';
        this.townData.forEach(function (_val,index) {
          if(row.areaCode==_val.areaCode){
            areaName = _val.areaName;
            return true;
          }
        });
        return areaName;
      },
    }
  };
</script>
